﻿using ApprovalFlow_WebApi.Controllers;
using DomainService.IService;
using Microsoft.AspNetCore.Mvc;
using Model.BusinessModel;
using Model.BusinessModel.InPut;
using Model.BusinessModel.OutPut;
using Model.DomainModel;
using Utility;
using Utility.Enum;

namespace ProjectManageWebApi.Controllers.examine
{
    /// <summary>
    /// Examine_FlowApply 流程申请
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "FlowApply")]
    public class FlowApplyController : BaseController
    {
        #region 构造实列化

        /// <summary>
        /// 流程服务
        /// </summary>
        public IFlowApplyService _flowApplyService;

        /// <summary>
        /// 流程消息服务
        /// </summary>
        private readonly IFlowMessageService _flowMessageService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="flowApplyService"></param>
        /// <param name="flowMessageService"></param>
        public FlowApplyController(IFlowApplyService flowApplyService, IFlowMessageService flowMessageService)
        {
            _flowApplyService = flowApplyService;
            _flowMessageService = flowMessageService;
        }

        #endregion

        #region 接口

        /// <summary>
        /// 新增流程申请信息
        /// </summary>
        /// <param name="FlowApply">实体模型</param>
        /// <returns></returns>
        [HttpPost]
        public ReceiveStatus Insert(FlowApply FlowApply)
        {
            FlowApply.CreateTime = DateTime.Now;
            FlowApply.CreateUserKey = GetLoginUserMsg().UserId.ToString();
            return _flowApplyService.Insert(FlowApply);
        }

        /// <summary>
        /// 更新流程申请信息
        /// </summary>
        /// <param name="FlowApply">实体模型</param>
        /// <returns></returns>
        [HttpPost]
        public ReceiveStatus Update(FlowApply FlowApply)
        {
            FlowApply.CreateTime = DateTime.Now;
            FlowApply.CreateUserKey = GetLoginUserMsg().UserId.ToString();
            return _flowApplyService.Update(FlowApply);
        }

        /// <summary>
        /// 获取所有流程申请
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ReceiveStatus<List<FlowApplyOutPut>> GetAllFlowApplyList(FlowApplyInput pageResultModel)
        {
            ReceiveStatus<List<FlowApplyOutPut>> receiveStatus = new();
            var result = _flowApplyService.GetAllFlowApply(pageResultModel);
            receiveStatus.data = result.data;
            receiveStatus.total = result.total;
            return receiveStatus;
        }

        /// <summary>
        /// 获取等待审批流程
        /// </summary>
        /// <param name="pageResultModel">分页模型</param>
        /// <returns></returns>
        [HttpPost]
        public ReceiveStatus<List<WaitApprovalFlowOutPut>> GetWaitApprovalFlow(FlowApplyInput pageResultModel)
        {
            ReceiveStatus<List<WaitApprovalFlowOutPut>> receiveStatus = new();
            var result = _flowApplyService.GetWaitApprovalFlow(pageResultModel);
            receiveStatus.data = result.data;
            receiveStatus.total = result.total;
            return receiveStatus;
        }

        /// <summary>
        /// 获取已审核审批流程
        /// </summary>
        /// <param name="pageResultModel">分页模型</param>
        /// <returns></returns>
        [HttpPost]
        public ReceiveStatus<List<AuditedApprovalFlowOutPut>> GetAuditedApprovalFlow(FlowApplyInput pageResultModel)
        {
            ReceiveStatus<List<AuditedApprovalFlowOutPut>> receiveStatus = new();
            var result = _flowApplyService.GetAuditedApprovalFlow(pageResultModel);
            receiveStatus.data = result.data;
            receiveStatus.total = result.total;
            return receiveStatus;
        }

        /// <summary>
        /// 获取抄送流程
        /// </summary>
        /// <param name="pageResultModel">分页模型</param>
        /// <returns></returns>
        [HttpPost]
        public ReceiveStatus<List<CarbonCopyFlowOutPut>> GetCarbonCopyFlow(FlowApplyInput pageResultModel) 
        {
            ReceiveStatus<List<CarbonCopyFlowOutPut>> receiveStatus = new();
            var result = _flowApplyService.GetCarbonCopyFlow(pageResultModel);
            receiveStatus.data = result.data;
            receiveStatus.total = result.total;
            return receiveStatus;
        }

        /// <summary>
        /// 根据申请id获取数据
        /// </summary>
        /// <param name="flowApplyId">主键</param>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<FlowApplyExtendOutPut> GetFlowApplyByApplyId(string flowApplyId)
        {
            return _flowApplyService.GetFlowApplyByApplyId(flowApplyId);
        }

        /// <summary>
        /// 更新发布状态
        /// </summary>
        /// <param name="flowApplyId">主键</param>
        /// <param name="isPublish">发布状态</param>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus UpdateIsPublish(string flowApplyId, bool isPublish)
        {
            return _flowApplyService.UpdateIsPublish(flowApplyId, isPublish);
        }

        /// <summary>
        /// 更新流程状态
        /// </summary>
        /// <param name="flowApplyId">流程id</param>
        /// <param name="flowApplyState">流程状态</param>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus UpdateFlowApplyState(string flowApplyId, FlowApplyStateEnum flowApplyState)
        {
            return _flowApplyService.UpdateFlowApplyState(flowApplyId, (int)flowApplyState);
        }

        /// <summary>
        /// 流程申请审核
        /// </summary>
        /// <param name="flowApplyExamineInput">审核信息</param>
        /// <returns></returns>
        [HttpPost]
        public ReceiveStatus FlowApplyExamine(FlowApplyExamineInput flowApplyExamineInput)
        {
            return _flowApplyService.FlowApplyExamine(flowApplyExamineInput, GetLoginUserMsg());
        }

        /// <summary>
        /// 获取申请当前节点前的所有节点数据（下拉框）
        /// </summary>
        /// <param name="flowApplyId">流程申请id</param>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<List<SelectOutPut>> GetExamineNodeSelectData(string flowApplyId)
        {
            return _flowApplyService.GetExamineNodeSelectData(flowApplyId);
        }

        /// <summary>
        /// 获取已审核节点数据
        /// </summary>
        /// <param name="flowApplyId"></param>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<List<FlowApplyRecord>> GetExamineNodeData(string flowApplyId)
        {
            return _flowApplyService.GetExamineNodeData(flowApplyId);
        }

        /// <summary>
        /// 获取当前登录人流程消息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<List<FlowMessage>> GetFlowMessage()
        {
            return _flowMessageService.GetFlowMessage();
        }

        /// <summary>
        /// 新增流程消息
        /// </summary>
        /// <param name="flowApplyId">流程申请id</param>
        /// <param name="recipientKeys">接收人员key</param>
        /// <param name="messageType">消息类型</param>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus InsertMessage(string flowApplyId, string recipientKeys, MessageTypeEnum messageType) 
        {
            return _flowMessageService.Insert(flowApplyId, recipientKeys, messageType);
        }
        #endregion
    }
}
